package com.example.cadmin.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cadmin.entity.ArchivedType;
import com.example.cadmin.service.ArchivedTypeService;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 归档类型 前端控制器
 * </p>
 *
 * @author  zzsoft 
 * @since 2023-02-15
 */
@Controller
@RequestMapping("/archived/type")
public class ArchivedTypeController {

    @Resource
    public ArchivedTypeService archivedTypeService;

    @GetMapping("/index")
    public String index(){
        return "ArchivedType/archived-list";
    }

    @ResponseBody
    @GetMapping("/list")
    public Map<String,Object> archivedTypeList(@RequestParam(value = "archivedName",required = false) String archivedName,
                                            @RequestParam(value = "page") int page,
                                            @RequestParam(value = "limit") int limit){
        return archivedTypeService.archivedTypeList(archivedName,page,limit);
    }

    @GetMapping("/add")
    public ModelAndView attributeAdd(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("ArchivedType/archived-add");
        return modelAndView;
    }

    @PostMapping("/save")
    @ResponseBody
    public ResponseModel archivedSave(@RequestBody JSONObject data) {
        if (StrUtil.isEmpty(data.getStr("archivedName"))) {
            return ResponseModel.fail("归档类型名为空");
        }
        return archivedTypeService.archivedSave(data);
    }

    @GetMapping("edit")
    public ModelAndView edit(@RequestParam int archivedId){
        Map<String,Object> fileAttribute = archivedTypeService.getMap(new QueryWrapper<ArchivedType>().eq("id",archivedId));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("ArchivedType/archived-edit");
        modelAndView.addAllObjects(fileAttribute);
        return modelAndView;
    }

    @PostMapping("editInfo")
    @ResponseBody
    public ResponseModel editInfo(@RequestBody JSONObject data){
        return archivedTypeService.editInfo(data);
    }

    @DeleteMapping("/remove/{archivedId}")
    @ResponseBody
    public ResponseModel remove(@PathVariable("archivedId") String archivedId){
        return archivedTypeService.remove(archivedId);
    }

}
